<?php

class Application_Model_OrderBooks extends Application_Model_Base{
    private static $instance = null;
    public static function getInstance(){
        if (null === self::$instance) self::$instance = new self();
        return self::$instance;
    }
    private function __clone() {}
    private function __construct() {}

    public function getPaginatorForFilter($params){
        $select = $this->getDBAdapter()->select()
            ->from('order_books');
        if (!empty($params['order_books_issue_date'])){
            $select->where("`order_books_issue_date` LIKE '%{$params['order_books_issue_date']}%'");
        }
        if(!empty($params['order_books_return_date'])){
            $select->where("`order_books_return_date` LIKE '%{$params['order_books_return_date']}%'");
        }
        if(!empty($params['order_books_books_id'])){
            $select->where("`order_books_books_id` LIKE '%{$params['order_books_books_id']}%'");
        }
        if(!empty($params['order_books_users_id'])){
            $select->where("`order_books_users_id` LIKE '%{$params['order_books_users_id']}%'");
        }
        if(!empty($params['order_books_max_duration'])){
            $select->where("`order_books_max_duration` LIKE '%{$params['order_books_max_duration']}%'");
        }
        $paginatorAdapter = new Zend_Paginator_Adapter_DbSelect($select);
        return new Zend_Paginator($paginatorAdapter);
    }

    public function getAll(){
        return $this->getDBAdapter()->query("
            select * from `order_books`
        ")->fetchAll();
    }

    public function getAllForSelect(){
        $bookOrders = $this->getDBAdapter()->query("
            select `order_book_id`, `order_book_name` from `order_books`
        ")->fetchAll();
        $bookOrdersForSelect = array();
        foreach($bookOrders as $bookOrder){
            $bookOrdersForSelect[$bookOrder['order_book_id']] = $bookOrder['order_book_name'];
        }
        return $bookOrdersForSelect;
    }

    public function add($data){
        $this->getDBAdapter()->insert('order_books', $data);
    }
}